//根据DOM元素的id构造出一个编辑器
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    lineNumbers: true,
    matchBrackets: true,
    mode: "text/x-groovy",
    theme: "dracula",	//设置主题
    lineWrapping: true,	//代码折叠
    foldGutter: true,
    gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
    matchBrackets: true
});
editor.setSize('auto','auto');
editor.setValue("");
layui.use(['layer', 'form'], function () {
    var layer = layui.layer
        , $ = layui.$
        , form = layui.form;
    var lineData = getLineData();
    //下拉框非空验证
    form.verify({
        formTextLength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255)){
                    sum=sum+1;
                } else {
                    sum=sum+2;
                }
            }
            if (sum > 200) {
                return '最多只能200个字符或者100个中文字';
            }
        }
    });
    $('.generalTemplate').on('click', function () {
        $.ajax({
            url: '/template/template.groovy',
            type: 'get',
            success: function (data) {
                editor.setValue(editor.getValue()+data);
            },
        });
    })
    $('.mysqlTemplate').on('click', function () {
        $.ajax({
            url: '/template/mysql.groovy',
            type: 'get',
            success: function (data) {
                editor.setValue(editor.getValue()+data);
            },
        });
    })
    $('.redisTemplate').on('click', function () {
        $.ajax({
            url: '/template/redis.groovy',
            type: 'get',
            success: function (data) {
                editor.setValue(editor.getValue()+data);
            },
        });
    })
    $('.closePage').on('click', function () {
        closeSonPage();
    });
    function closeSonPage() {
        parent.layer.closeAll();
    }
    //监听提交
    form.on('submit(formSubmit)', function (data) {
        var paramData = data.field;
        console.log(paramData);
        var code=editor.getValue();
        var name = paramData.scriptName.indexOf('.groovy')>-1?paramData.scriptName.split('.groovy')[0]:paramData.scriptName
        if (name==null||name==""){
            layer.msg("文件名填写不规范");
            return ;
        }
        if (code==null||code==""){
            layer.msg("请输入脚本内容");
            return ;
        }
        if(!$(this).attr('lock')||$(this).attr('lock') == "false"){
            $(this).attr('lock',true);
            var url="";
            var param={};
            if(lineData!= null){
                url='/groovy/editScript';
                param={ name: name+'.groovy',code:code,cname:lineData.serviceName};
            }else{
                url='/groovy/addScript';
                param={ name: name+'.groovy',code:code};
            }
            $.ajax({
                url: url,
                type: 'post',
                data: param,
                contentType: 'application/x-www-form-urlencoded',
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg, { time: 900, icon: 6 }, function () {
                            window.parent.renderTable();
                            closeSonPage();
                        });
                        return;
                    } else {
                        layer.open({
                            type: 1
                            ,offset: "offset"
                            ,id: 'layerDemo' //防止重复弹出
                            ,content: '<div style="padding: 20px 100px;">'+ data.msg +'</div>'
                            ,btn: '关闭'
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0 //不显示遮罩
                            ,yes: function(){
                                layer.closeAll();
                            }
                        });
                        return;
                    }
                },
            });
            $(this).attr('lock',false);
        }
        return false;
    });
    function getCode() {
        if (lineData!= null) {
            $.ajax({
                url: '/groovy/getScript',
                type: 'post',
                data: { name: lineData.serviceName,path:lineData.scriptPath},
                contentType: 'application/x-www-form-urlencoded',
                success: function (data) {
                    if (data.code == 200) {
                        $('input[name="scriptName"]').val(lineData.serviceName.split(".groovy")[0]);
                        editor.setValue(data.data);
                    } else {
                        layer.msg(data.msg, { icon: 5 });//失败的表情5 成功的表情6
                        return;
                    }
                },
            });
        }else{
            $.ajax({
                url: '/template/template.groovy',
                type: 'get',
                success: function (data) {
                    editor.setValue(data);
                },
            });
        }
    }
    getCode();
});

